
import time
from datetime import datetime
import redis
from setting import TIME_DIFFERNCE

class AtctTimeMethod:

    def __init__(self, atct= '242301000000') -> None:

        self.atct = atct

    @staticmethod
    def parse_date_month(hex_str): # 解释时间月份
        '''
        >>> parse_date_month('32')
        >>> {'week': 1, 'month': 12}
        
        '''
        binary_string = bin(int(hex_str, 16))[2:]

        for i in range(8 - len(binary_string)): # 补成8位

            binary_string = '0' + binary_string
        
        # 提取日期的各个部分
        week = int(binary_string[:3])
        month = int(binary_string[3]) * 10 + int(binary_string[4:], 2)
        
        return {"week": week, "month": month}

    def parse_date(self):

        year = 2000 + int(self.atct[:2])
        month = self.parse_date_month(self.atct[2:4])["month"]
        day = int(self.atct[4:6])
        hour = int(self.atct[6:8])
        minute = int(self.atct[8:10])
        second = int(self.atct[10:12])

        return f"{year}-{month}-{day} {hour}:{minute}:{second}"
    
    def parse_timestamp(self):

        time_string = self.parse_date()

        # 解析时间字符串
        datetime_obj = datetime.strptime(time_string, "%Y-%m-%d %H:%M:%S")

        # 将时间对象转换为时间戳
        timestamp = datetime_obj.timestamp()

        print(time_string)
        print(timestamp)

        return timestamp
    
    def set_time_difference(self): # 设置时间差值
    
        self.time_differnce = time.time() - self.parse_timestamp()

        self.r = redis.Redis(host='localhost', port=6379, db=0)

        self.r.set(TIME_DIFFERNCE, self.time_differnce)

        
    
if __name__ == "__main__":

    tf = AtctTimeMethod()

    print(tf.parse_date())



           